package com.esteel.coroutine.viewmodel

import android.app.Application
import android.arch.lifecycle.AndroidViewModel
import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * Created by Android Studio.
 * User: 赵宏图
 * Date: 2019/8/21
 * Time: 9:56
 * Email:zhaohongtu@payworth.cn
 * Desc:
 */
class MainViewModel : ViewModel() {

    private val liveData: MutableLiveData<String> = MutableLiveData()

    //缺点:只能suspend函数才能调用
//    suspend fun onGetInfo () {
//        val deferred = GlobalScope.async {
//            delay(1000)
//
//            return@async "赵宏图"
//        }
//        liveData.postValue(deferred.await())
//    }

    fun onGetInfo() {
        GlobalScope.launch {
            val deferred = async {
                delay(1000)

                return@async "徐雪娅"
            }

            liveData.postValue(deferred.await())
        }
    }

    fun getLiveData(): MutableLiveData<String> {
        return liveData
    }
}